<%--
  Created by IntelliJ IDEA.
  User: yuan
  Date: 2022/11/23
  Time: 21:22
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>更新Emp</title>
    <base href="<%=request.getContextPath()+"/"%>">
    <script type="text/javascript" src="js/jquery.js"></script>
    <script type="text/javascript">
        $(function () {
            $("#btnUpdate").click(function () {
                //统一获取表单数据并转为json
                let data = {};
                $($("#form").serializeArray()).each(function () {
                    data[this.name] = this.value;
                });
                let formData = JSON.stringify(data);
                $.post(
                    "http://localhost:8080<%=request.getContextPath()%>/EmpServlet/updateEmpAsync",
                    formData,
                    function (data, status) {
                        let result = JSON.parse(data);
                        if (result.code===200 && status==="success") {
                            window.location.href = "http://localhost:8080<%=request.getContextPath()%>/EmpServlet/list"
                        } else {
                            alert(result.msg);
                        }
                    });
            });
        });
    </script>
</head>
<body>
<form method="post" action="EmpServlet/update" id="form" onsubmit="return false">
    <input type="hidden" id="empno" name="empno" value="${empOne.empno}">
    <table border="1">
        <tr>
            <td>姓名</td>
            <td><input type="text" id="ename" name="ename" value="${empOne.ename}"></td>
        </tr>

        <tr>
            <td>工资</td>
            <td><input type="text" id="sal" name="sal" value="${empOne.sal}"></td>
        </tr>

        <tr>
            <td>奖金</td>
            <td>
                <input type="text" id="comm" name="comm" value="${empOne.comm}">
            </td>
        </tr>

        <tr>
            <td>职位</td>
            <td>
                <input type="text" id="job" name="job" value="${empOne.job}">
            </td>
        </tr>

        <tr>
            <td>领导</td>
            <td>
                <input type="text" id="mgr" name="mgr" value="${empOne.mgr}">
            </td>
        </tr>

        <tr>
            <td>部门</td>
            <td>
                <select name="deptno" id="deptno">
                    <option value="10" ${empOne.deptno==10?selected:null}>ACCOUNTING</option>
                    <option value="20" ${empOne.deptno==20?selected:null}>RESEARCH</option>
                    <option value="30" ${empOne.deptno==30?selected:null}>SALES</option>
                    <option value="40"  ${empOne.deptno==40?selected:null}>OPERATIONS</option>
                </select>
            </td>
        </tr>

        <tr>
            <td>入职时间</td>
            <td>
                <input type="date" id="hiredate" name="hiredate" value="${empOne.hiredate}">
            </td>
        </tr>

        <tr>
            <td colspan="2">
                <button id="btnUpdate">更新</button>
            </td>
        </tr>

    </table>

</form>
</body>
</html>
